﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Ekton_Manage_Framework.Methods
{
    public class SerializeParameter
    {
        /// <summary>
        /// 将参数转化为字典集合
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<Dictionary<string, object>> ParameConvertToDict(string param)
        {
            Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
            if (string.IsNullOrEmpty(param) || string.IsNullOrEmpty(param.Replace("{", string.Empty).Replace("}", string.Empty).Replace("\"", string.Empty)) || param == "null")
                goto code;
            param = param.Replace("{", string.Empty).Replace("}", string.Empty).Replace("\"", string.Empty);
            var strArray = param.Split(',');
            foreach (var item in strArray)
            {
                var result = item.Split(":");
                if (result.Length < 2)
                    throw new Exception("Insufficient array length");
                else if (result.Length == 2)
                    keyValuePairs.Add(result[0], result[1]);
                else //Datetime
                {
                    StringBuilder MyStringBuilder = new StringBuilder();
                    for (int i = 1; i < result.Length; i++)
                    {
                        if (i == result.Length - 1)
                            MyStringBuilder.Append($"{result[i]}");
                        else
                            MyStringBuilder.Append($"{result[i]}" + ":");
                    }
                    keyValuePairs.Add(result[0], MyStringBuilder.ToString());
                }
            }
        code: return keyValuePairs;
        }
    }
}